<template>
  <div class="employees">
    <el-card>
      <page-tools :show-left="false">
        <template #right>
          <div>
            <el-button
              v-setButtonPower="'employees_importButton'"
              type="primary"
              @click="goUpload"
            >导入</el-button>
            <el-button type="primary" @click="addclick">+ 新增员工</el-button>
          </div>
        </template>
      </page-tools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="员工头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-imgError="defaultImg"
              class="img"
              :src="row.staffPhoto ? row.staffPhoto : defaultImg"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <!-- <el-table-column
          label="聘用形式"
          prop="formOfEmployment"
          :formatter="formatter"
          width="80"
        /> -->
        <el-table-column label="聘用形式" prop="formOfEmployment" width="80" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职日期"
          prop="timeOfEntry"
          :formatter="formattertimeOfEntry"
        />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="page">
      <el-pagination
        :current-page="page.page"
        :page-sizes="[1, 2, 3, 10, 100, 200, 300, 400]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <Add ref="add" @getData="getData" />
    <!-- <Excel ref="upload" @getData="getData" /> -->
    <!-- 头像二维码 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <!-- 分配角色弹框 -->
    <Role ref="role" v-model="showRole" />
    <!-- <Role ref="role" :is-show="showRole" @close="close" /> -->
  </div>
</template>
<script>
import { sysUser, sysUserdel } from '@/api/employees'
import employeesData from '@/api/constant/employees.js'
import Add from './components/add.vue'
import Role from './components/role.vue'
import dayjs from 'dayjs'
import Qrcode from 'qrcode'
// import Excel from './components/excel.vue'
export default {
  name: '',
  components: { Add, Role },
  data() {
    return {
      showRole: false,
      showQrcode: false,
      defaultImg: require('@/assets/common/head.jpg'),
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 100
      }
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    // 每页显示多少条
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    // 当前页码
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 获取员工列表
    async getData() {
      const res = await sysUser(this.page)
      console.log(res, 123)
      this.page.total = res.data.total
      // 从数据原头上进行数据转换
      res.data.rows.forEach((item) => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          item.formOfEmployment
        )
      })
      this.list = res.data.rows
    },
    // 格式化聘用形式
    formatter(row, column, cellValue, index) {
      const temp = employeesData.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },
    // 点击显示新增员工对话框
    addclick() {
      this.$refs.add.dialogVisible = true
    },
    // 格式化入职时间
    formattertimeOfEntry(row, column, cellValue, index) {
      // console.log(cellValue)
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    del(row) {
      this.$confirm('确定要删除该员工吗？', '提示').then(async() => {
        await sysUserdel(row.id)
        this.$message.success('删除成功')
        // this.handleCurrentChange(1)
        if (this.page.page > 1 && this.list.length === 1) {
          this.page.page--
        }
        this.getData()
      })
    },
    // 点击导入
    goUpload() {
      // 点击导入跳转页面， 传入name的参数
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
      // this.$refs.upload.dialogVisible = true
    },
    // 点击查看
    look(row) {
      this.$router.push('/employees/detail/' + row.id)
    },
    // 点击头像
    imgClick(imgSrc) {
      console.log(imgSrc, 8888)
      if (!imgSrc) {
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgSrc, { width: 400, height: 400 })
      })
    },
    // 点击角色按钮 弹出分配角色框
    roleClick(row) {
      this.showRole = true
      this.$refs.role.getUserInfo(row.id)
    }
    // close() {
    //   this.showRole = false
    // }
  }
}
</script>
<style lang="scss" scoped>
.employees {
  padding: 15px;
  .page {
    margin-top: 20px;
    text-align: right;
  }
  .img {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
